package com.haogu.service.impl;

import com.haogu.entity.Employee;
import com.haogu.entity.PEmployee;
import com.haogu.entity.Position;
import com.haogu.entity.User;
import com.haogu.mapper.EmployeeMapper;
import com.haogu.mapper.PositionMapper;
import com.haogu.mapper.UserMapper;
import com.haogu.service.EmployeeService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;
    
    @Resource
    private UserMapper userMapper;
    @Resource
    private PositionMapper positionMapper;
    /**
     * 去添加员工页面
     * @param model
     */
    @Override
    public void toAddEmployee(Model model) {
        List<User> users = userMapper.selectList(null);
        List<Position> positions = positionMapper.selectList(null);
        model.addAttribute("users",users);
        model.addAttribute("positions",positions);
    }

    /**
     * 添加员工
     * @param employee
     * @param user
     * @return
     */
    @Override
    public Map addEmployee(Employee employee, User user) {
        employee.setCreateUserId(user.getId());
        int insert = employeeMapper.insert(employee);
        Map map =new HashMap();
        if(insert==0){
            map.put("success",false);
            map.put("message","添加失败");
        }else{
            map.put("success",true);
            map.put("message","添加成功");
        }
        return map;
    }

    /**
     * 分页展示数据
     * @param page
     * @param rows
     * @param name
     * @return
     */
    @Override
    public Map listEmployee(Integer page, Integer rows,String name) {
        Map map = new HashMap();
        List<PEmployee> pEmployees = employeeMapper.listEmployee((page - 1) * rows, rows,name);
        Integer integer = employeeMapper.selectCount(null);
        map.put("totalRows",integer);
        map.put("result",pEmployees);
        Map map1 = new HashMap();
        map1.put("data",map);
        return map1;
    }

    @Override
    public void toUpdateEmployee(Integer id, Model model) {
        Employee employee = employeeMapper.selectById(id);
        List<User> users = userMapper.selectList(null);
        List<Position> positions = positionMapper.selectList(null);
        model.addAttribute("users",users);
        model.addAttribute("positions",positions);
        model.addAttribute("employee",employee);
    }

    @Override
    public Map updateEmployee(Employee employee, User user) {
        employee.setUpdateUserId(user.getId());
        int i = employeeMapper.updateById(employee);
        Map map = new HashMap();
        if(i==0){
            map.put("success",false);
            map.put("message","修改失败");
        }else{
            map.put("success",true);
            map.put("message","修改成功");
        }
        return map;
    }

    @Override
    public Map deleteEmployee(String ids) {
        //对传过来的ids进行操作
        String[] split = ids.split(",");
        System.out.println(ids);
        System.out.println(split);
        boolean flag = true;
        for(int i = 0; i < split.length;i++){

            int i1 = employeeMapper.deleteById(Integer.parseInt(split[i]));

            if(i1==0){
                flag = false;
            }
        }
        Map map = new HashMap();
        if(flag){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }
}
